package com.etms.service.impl;


import com.etms.mapper.AttendMapper;
import com.etms.mapper.RepertoryMapper;
import com.etms.mapper.TuihuoMapper;
import com.etms.pojo.*;
import com.etms.service.*;
import com.etms.utils.DaochuUtils;
import com.etms.utils.DateUtils;
import com.etms.utils.IdUtils;
import com.etms.vo.*;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ChukuServiceImpl implements ChukuService {
    @Autowired
    RepertoryMapper repertoryMapper;
    @Autowired
    HuopinService huopinService;
    @Autowired
    DeptService deptService;
    @Autowired
    SupplierService supplierService;
    @Autowired
    StatusService statusService;
    @Autowired
    CustomerService customerService;
    @Autowired
    AttendMapper attendMapper;
    @Autowired
    ThreadService threadService;
    @Override
    public Result updateOrAdd(RukuParmas rukuParmas, Integer uid) {
        Integer id = rukuParmas.getId();

        int dId= IdUtils.getId(rukuParmas.getDept());
        int gId= IdUtils.getId(rukuParmas.getName());
        int wid = IdUtils.getId(rukuParmas.getWid());
        Integer keId=IdUtils.getId(rukuParmas.getCustomer());;
        Integer sid=4;//销售出库
        Integer ddId=3;//总部门id: 销售部

        long sum = rukuParmas.getSum();
        String dateTime = DateUtils.getDateTime(new Date());
        Repertory repertory=new Repertory();
        repertory.setGid(gId).setId(id).setWId(wid).setDdId(ddId).setDeptId(dId).setSid(sid).setKeId(keId).setSum(sum).setUpdateTime(dateTime).setRuChu(2);
        if(id!=null&&id!=0) {//走更新
            repertoryMapper.update(repertory);
            return Result.successed("修改成功!",null);
        }
        else repertoryMapper.addOne(repertory);
        //更新个人工作量,异步更新
        threadService.updateAttend(attendMapper,uid);
        return Result.successed("出库成功!",null);
    }

    @Override
    public Result getList(PageParmas pageParmas) {
        PageHelper.startPage(pageParmas.getCurrentPage(), pageParmas.getPageSize());
        List<Repertory>list=repertoryMapper.findAll2(pageParmas.getQueryMsg());
//        if(pageParmas.getQueryMsg().length()>0&&pageParmas.getQueryMsg()!=""&&pageParmas.getQueryMsg()!="0"){
//            list=repertoryMapper.findAll2(pageParmas.getQueryMsg());
//        }
//        else
//        list=repertoryMapper.findAll2(pageParmas.getQueryMsg());
        PageInfo pageInfo=new PageInfo(list);
        PageResult pageResult=new PageResult();
        List<RepertoryVo>repertoryVolist=copyList(list);
        pageResult.setList(repertoryVolist);
        pageResult.setTotalPage(pageInfo.getPages());
        return Result.successed(pageResult);
    }

    @Override
    public Result getById(Integer id) {
        Repertory repertory=repertoryMapper.findById(id);
        RepertoryVo repertoryVo=copy(repertory);
        return Result.successed(repertoryVo);
    }

    @Override
    public Result del(Integer id) {
        boolean f=repertoryMapper.delById(id);
        if(!f){
            return Result.failed("删除失败,当前信息可能已被删除过");
        }
        return Result.successed("删除成功",null);
    }

    @Autowired
    TuihuoMapper tuihuoMapper;

    @Override
    public Result tuihuo(Integer id) {
        Integer rsid = repertoryMapper.findById(id).getSid();//得到状态码
        Tuihuo tuihuo=new Tuihuo();
        tuihuo.setIspass(false);
        tuihuo.setReId(id);
        tuihuo.setUpdateTime(DateUtils.getDateTime(new Date()));
        tuihuo.setSid(rsid+10);//规定每种入库/出库方式对应的退货状态码是在此基础上面加十
        tuihuo.setRuChu(2);//出
        int cnt=0;
        try {
            cnt=tuihuoMapper.addOne(tuihuo);
        }catch (Exception e){
            e.printStackTrace();
        }
        if (cnt==0)return Result.failed("不能重复提交退货");

        return Result.successed("退货成功!等待审核!",null);
    }

    @Override
    public Result getTuiList() {
        List<Tuihuo> list = tuihuoMapper.findAll2();
        List<TuihuoVo>tuihuoVos=new ArrayList<>();
        for (Tuihuo tuihuo : list) {
            TuihuoVo tuihuoVo=new TuihuoVo();
            BeanUtils.copyProperties(tuihuo,tuihuoVo);
//            tuihuoVo.setIspass(false);
            tuihuoVo.setStatus(statusService.findById(tuihuo.getSid()));
            tuihuoVos.add(tuihuoVo);
        }
        return Result.successed(tuihuoVos);
    }

    @Override
    public Result quxiaotuihuo(Integer id) {
        int cnt=tuihuoMapper.delById(id);
        if(cnt==0){
            return Result.failed("发起失败!请稍后重试");
        }
        return Result.successed("成功取消!");
    }

    @Override
    public Result daochuById(Integer id) {
        Repertory repertory=repertoryMapper.findById(id);
        RepertoryVo repertoryVo=copy(repertory);

        String filename = DaochuUtils.daochuxiaoshou(id, repertoryVo);
        return Result.successed("导出信息成功!导出文件地址为: "+filename,null);
    }

    @Override
    public Result tuihuo2(TuihuoParmas tuihuoParmas) {//部分退货
        Integer id = tuihuoParmas.getId();
        Integer counts = tuihuoParmas.getCounts();//退货数量
        Integer rsid = repertoryMapper.findById(id).getSid();
        Tuihuo tuihuo=new Tuihuo();
        tuihuo.setIspass(false).setReId(id).setUpdateTime(DateUtils.getDateTime(new Date())).setSid(rsid+10).setRuChu(2).setCounts(counts);
        //规定每种入库/出库方式对应的退货状态码是在此基础上面加十  1入
        int cnt=0;
        try {
            cnt=tuihuoMapper.addOne2(tuihuo);//部分退货
        }catch (Exception e){
            e.printStackTrace();
        }
        if (cnt==0)return Result.failed("不能重复提交退货");

        return Result.successed("退货成功!等待审核!",null);
    }

    private List<RepertoryVo> copyList(List<Repertory> list) {
        List<RepertoryVo> copyList=new ArrayList<>();
        for (Repertory repertory : list) {
            RepertoryVo repertoryVo=copy(repertory);
            copyList.add(repertoryVo);
        }
        return copyList;
    }

    private RepertoryVo copy(Repertory repertory) {
        RepertoryVo vo=new RepertoryVo();
        BeanUtils.copyProperties(repertory,vo);
        Integer gid = repertory.getGid();//分类id
        Integer ddId = repertory.getDdId();//总部门id
        Integer deptId = repertory.getDeptId();//部门id
        Integer keId = repertory.getKeId();//客户id
        Integer sid=repertory.getSid();//状态属性id
        Integer wId = repertory.getWId();
        Customer customer=new Customer();
        Goods_category good= (Goods_category) huopinService.findById(gid).getData();
        Dept dept=deptService.findXiaoById(deptId);;

        if(keId!=null)customer=customerService.findById(keId);

        Goods_status goods_status=statusService.findById(sid);
        vo.setGood(good).setDept(dept).setCustomer(customer).setStatus(goods_status).setWid(wId);

        vo.setTotalPrice(IdUtils.doubleToString(good.getPerPrice()*repertory.getSum()));
        return vo;
    }
}
